﻿using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Application.IO;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Cms.Tests.Unit.Application.Admin.Presenters
{
    [TestClass]
    public class EditCustomAdminPageTests : BasePresenterTests
    {
        private Administrator _admin;

        [TestMethod]
        public void Save_DuplicateFilename_WarningIssuedNothingSaved()
        {
            _admin = new Administrator();
            AdministratorGroup group = new AdministratorGroup { DefaultPageAccess = DefaultAccess.Allow };
            _admin.Group = group;
            CustomAdminPage editEmployee = new CustomAdminPage() { Id = 1, Name = "Edit employee", Filename = "EditEmployee.aspx" };
            InitRepositoryFactoryMock();
            AdministratorRepositoryMock.Setup(a => a.GetAdministrator(1)).Returns(_admin);
            SystemRepositoryMock.Setup(m => m.GetCustomAdminPage(1)).Returns(editEmployee);
            SystemRepositoryMock.Setup(m => m.CustomAdminPageFilenameExists(-1, "EditEmployee.aspx")).Returns(true);
            Mock<IEditCustomAdminPageView> viewMock = new Mock<IEditCustomAdminPageView>();
            viewMock.Setup(v => v.Localization).Returns("en-US");
            viewMock.Setup(v => v.ContextTranslationFile).Returns("Admin/Translations/EditCustomAdminPage.xml");

            // Setup view as a new CustomAdminPage
            viewMock.SetupGet(m => m.AdministratorID).Returns(1);
            viewMock.SetupGet(m => m.CustomAdminPageId).Returns(-1);
            viewMock.SetupGet(m => m.CurrentTab).Returns("properties");
            viewMock.SetupGet(m => m.Name).Returns("Edit x");
            viewMock.SetupGet(m => m.Filename).Returns("EditEmployee");
            Mock<IDiskAccess> diskAccessMock = new Mock<IDiskAccess>();
            EditCustomAdminPagePresenter presenter = new EditCustomAdminPagePresenter(viewMock.Object, FactoryMock.Object);
            presenter.DiskAccess = diskAccessMock.Object;
            presenter.Save();
            Assert.IsTrue(presenter.ValidationErrorCodes.Contains(3));
            SystemRepositoryMock.Verify(m => m.SaveCustomAdminPage(It.IsAny<CustomAdminPage>()), Times.Never());
        }
    }
}
